package edu.upc.clase.demo.controller;

import edu.upc.clase.demo.entity.CriterioBusqueda;
import edu.upc.clase.demo.entity.Subcategoria;
import edu.upc.clase.demo.service.SubcategoriaService;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Carlos
 */
@Controller("subcategoriaController")
public class SubcategoriaController {

    @Autowired
    private SubcategoriaService subcategoriaService;
    private static Logger log = LoggerFactory.getLogger(SubcategoriaController.class);

    @RequestMapping("/subcategorias/index")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView("subcategorias/index");
        List<Subcategoria> subcategorias = subcategoriaService.buscarTodos();
        CriterioBusqueda criterioBusqueda = new CriterioBusqueda();
        mav.addObject("subcategorias", subcategorias);
        mav.addObject("criterioBusqueda",criterioBusqueda);
        return mav;
    }
    
    @RequestMapping(value = "/subcategorias/new", method = RequestMethod.GET)
    public ModelAndView newSubcategoria() {
        ModelAndView mav = new ModelAndView("subcategorias/new");
        Subcategoria subcategoria = new Subcategoria();
        mav.getModelMap().put("subcategoria", subcategoria);
        return mav;
    }

    @RequestMapping(value = "/subcategorias/new", method = RequestMethod.POST)
    public String createSubcategoria(@ModelAttribute("subcategoria")Subcategoria subcategoria, SessionStatus status) {    
        subcategoriaService.insertar(subcategoria);
        status.setComplete();
        return "redirect:/pages/subcategorias/index";
    }
    
    @RequestMapping(value = "/subcategorias/edit", method = RequestMethod.GET)
    public ModelAndView editSubcategoria(@RequestParam("id")Integer id) {    
        ModelAndView mav = new ModelAndView("subcategorias/edit");
        Subcategoria subcategoria = subcategoriaService.buscar(id);
        mav.getModelMap().put("subcategoria", subcategoria);
        return mav;        
    }
    
    @RequestMapping(value="/subcategorias/edit", method=RequestMethod.POST)
    public String update(@ModelAttribute("subcategoria") Subcategoria subcategoria, SessionStatus status) {
        subcategoriaService.actualizar(subcategoria);
        status.setComplete();
        return "redirect:/pages/subcategorias/index";
    }
    
    @RequestMapping("/subcategorias/delete")
    public ModelAndView delete(@RequestParam("id")Integer id)
    {
        ModelAndView mav = new ModelAndView("redirect:/pages/subcategorias/index");
        Subcategoria subcategoria = subcategoriaService.buscar(id);
        subcategoriaService.eliminar(subcategoria);
        return mav;
    }
    
    @RequestMapping(value = "/subcategorias/buscar", method=RequestMethod.POST)
    public ModelAndView buscar(@ModelAttribute("criterioBusqueda") CriterioBusqueda criterio, SessionStatus status) {
        ModelAndView mav = new ModelAndView("subcategorias/resultadoBusqueda");
        log.info("resultado = " + subcategoriaService.buscarPorNombre(criterio.getNombre()).size());
        mav.getModel().put("resultado",subcategoriaService.buscarPorNombre(criterio.getNombre()));
        return mav;
    }
    
}
